package com.duoduo;

import org.omg.CORBA.SystemException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLNonTransientException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: algorithm
 * @description: test
 * @author: chaidl  ！
 * @create: 2022-05-05 21:23
 */
public class Test {

	static class User{
		private String name;
		private String code;

	}
	public static void main(String[] args) throws Exception {
		String s1="123";
		String s2=new String("123");
		System.out.println(s1==s2);
		Field value = s1.getClass().getDeclaredField("value");
		value.setAccessible(true);
		char[] s1Arr= (char[]) value.get(s1);
		s1Arr[0]="2".toCharArray()[0];
		System.out.println(s1);
		System.out.println(s2);

	}
	static int testa(){
		int a=1;
		try {
		    return a;
		}finally {
		    ++a;
		}
	}
	/**
	 * 从输入流中获取数据
	 * @param inStream 输入流
	 * @return
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream inStream) throws IOException {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len=inStream.read(buffer)) != -1 ){
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}
	//Date date = com.ziroom.sms.foundation.commons.util.DateUtils.formatStr2Date("20220511", "yyyyMMdd");
	//	System.out.println(date);
	//LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
	//	System.out.println(localDateTime);
	//int value = localDateTime.getDayOfWeek().getValue();
	//	System.out.println(value);
	/**
	 * String转LocalDateTime
	 *
	 * @param str
	 * @param format
	 * @return
	 */
	public static LocalDateTime parse(String str, String format) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
		return LocalDateTime.parse(str, df);
	}

	public static Date formatStr2Date(String str, String pattern) throws ParseException {
		Date d = null;
		if (null != str && !"".equals(str.trim())) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			d = sdf.parse(str);
		}
		return d;
	}

	/**
	 * 每月的所有日期  即1日到31日
	 *
	 * @param localDate 当地日期
	 * @return {@link List<LocalDate>}
	 */
	public static List<LocalDate> allDaysOfMonth(LocalDate localDate) {
		List<LocalDate> allDays = new ArrayList<>();
		LocalDate firstDayOfMonth = firstDayOfMonth(localDate);
		LocalDate lastDayOfMonth = lastDayOfMonth(localDate);
		allDays.add(firstDayOfMonth);
		int i = 1;
		LocalDate temp = firstDayOfMonth;
		while (!temp.isEqual(lastDayOfMonth)) {
			LocalDate day = firstDayOfMonth.plusDays(i);
			allDays.add(day);
			temp = day;
			i++;
		}
		return allDays;
	}

	/**
	 * 月的第一天
	 *
	 * @param localDate 当地日期
	 * @return {@link LocalDate}
	 */
	public static LocalDate firstDayOfMonth(LocalDate localDate) {
		return localDate.with(TemporalAdjusters.firstDayOfMonth());
	}

	/**
	 * 月的最后一天
	 *
	 * @param localDate 当地日期
	 * @return {@link LocalDate}
	 */
	public static LocalDate lastDayOfMonth(LocalDate localDate) {
		return localDate.with(TemporalAdjusters.lastDayOfMonth());
	}
	public void get() {
		LinkedList<Stock> buyList = new LinkedList<>();
		List<Stock> sellList = new ArrayList<>();
		Iterator<Stock> iterator = buyList.iterator();
		for (Stock stock : sellList) {
			BigDecimal share = stock.getShare();
			while (iterator.hasNext()) {
				Stock buyStock = iterator.next();
				if (stock.getDate()>=buyStock.getDate()){
					share = share.subtract(buyStock.getShare());
					if (share.compareTo(BigDecimal.ZERO) > 0) {
						iterator.remove();
					} else if (share.compareTo(BigDecimal.ZERO) == 0) {
						iterator.remove();
						break;
					} else {
						buyStock.setShare(new BigDecimal("0").subtract(share));
					}
				}
			}
		}
		BigDecimal totalShare = new BigDecimal("0");
		BigDecimal totalAmount = new BigDecimal("0");
		while (iterator.hasNext()) {
			Stock buyStock = iterator.next();
			totalShare.add(buyStock.getShare());
			totalAmount.add(buyStock.getShare().multiply(buyStock.getPrice()));
		}
	}
}
